package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**聊天室的客户端*/
public class Client3 {
    private Socket socket;
    public Client3() {
        try {
            /*
            实例化Socket时就是与远程服务器建立连接的过程
            需要传递两个参数：
            1.服务器的ip地址，用于找到网络上对方的计算机
            2、服务端口 用于找到服务器计算机上的服务端的应用程序（软件）

             */
            System.out.println("正在连接服务器");
            socket=new Socket("176.61.4.142",8088);//刘德欢
//            socket=new Socket("176.61.4.10",2333);

//            socket = new Socket("localhost", 8088);//localhost表示本机ip

            System.out.println("连接服务器成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start() {
            Thread t1 = new Thread() {
                public void run() {
                    try {
                        OutputStream out = socket.getOutputStream();//低级流，作用：将写出的字节发送给建立连接的远程计算机
                        OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
                        BufferedWriter bw = new BufferedWriter(osw);
                        PrintWriter pw = new PrintWriter(bw, true);
                        Scanner scanner = new Scanner(System.in);
                        while (true) {
                            String line = scanner.nextLine();
                            if ("exit".equals(line)) {
                                break;
                            }
                            pw.println(line);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            socket.close();//与服务器断开
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };

                Thread t2 = new Thread() {
                    public void run() {
                        try {
                            //V3:通过socket获取输入流用于读取服务端发送过来的消息
                            InputStream in = socket.getInputStream();
                            InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                            BufferedReader br = new BufferedReader(isr);
                            //v3:读取来自服务端发送过来的消息
                            String line ;
                           while((line=br.readLine())!=null){
                               System.out.println(line);
                           }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                socket.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }

                };
                t1.start();
                t2.start();
    }

    public static void main(String[] args) {
        Client3 client3=new Client3();
        client3.start();
    }
}